#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>


int main()
{
  char command[128];
  while(1)
  {
    printf("[name@VM-24-6-centos miniShell]$ ");
    fflush(stdout);

    command[0] = 0;
    fgets(command, 128, stdin); // ls\n\0
    command[strlen(command) - 1] = 0;

    char* comm[10] = {NULL};

    comm[0] = strtok(command, " ");

    int i = 1;
    while((comm[i] = strtok(NULL, " ")) != NULL)
    {
      ++i;
    }

    //for(i = 0; comm[i]; ++i)
    //{
    //  printf(comm[i]);
    //  printf("\n");
    //}

    if(strcmp(comm[0], "cd") == 0)
    {
      if(comm[1])
        chdir(comm[1]);
      
      continue;
    }

    
    if(fork() == 0)
    {
      execvp(comm[0], comm);
      exit(1);
    }

    waitpid(-1, NULL, 0);   
  }

  return 0;
}
